from flask import Blueprint,request,jsonify
import time
from model.model import *
from datetime import datetime
tongji=Blueprint('/tongji',__name__)
from Redis import temp_cache

@tongji.route('/tongji')
def Tong_Price():
    data=0
    total=0
    date = str(datetime.now())
    date=date[5:7]
    temp_time = Order.query.filter(Order.datetime,Order.price).all()
    for i in range(len(temp_time)):
        data=temp_time[i].datetime
        price=temp_time[i].price
        ttt=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(data)))
        data=ttt[5:7]
        if  data==date:
            print(f"\n本月收入+{price}")
            total+=price

        else:
            pass
    print(f'\n总收入:{total}\n')


    return ({'code':200,data:total})


import arrow

@tongji.route('/timedate')
def occupancy():
    print('当前时间' ,time)
    year_now=arrow.now().year
    start_time=arrow.get(f'{year_now}-01-01 00:00:00','YYYY-MM-DD HH:mm:ss')
    end_time=arrow.get(f'{year_now}-12-31 24:00:00','YYYY-MM-DD HH:mm:ss')
    print(year_now)
    print(end_time.int_timestamp)
    print(start_time)
    print(end_time)
    interval=end_time-start_time
    total=Order.query.count()*interval.days
    orders = Order.query.filter(Order.datetime < end_time.int_timestamp, Order.end_datetime > start_time.int_timestamp).all()
    print()
    days = 0
    for order in orders:
        days += (int(order.end_datetime) - int(order.datetime))/60/60/24
        print(days)

    return '%.3f' % (days * 100 / total) + '%'
